//
//  DaTiListView.m
//  Learn
//
//  Created by apple on 2024/4/5.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "DaTiListView.h"

@interface DaTiListTableCell : UITableViewCell
/* <#描述#> */
@property (nonatomic, strong) UIView    * bgView;
@property (nonatomic, strong) UILabel    * titleLabel;
@property (nonatomic, strong) UIImageView    * stateImg;

@end

@implementation DaTiListTableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
        self.contentView.backgroundColor = UIColor.whiteColor;
    }
    return self;
}
- (void)createUI {
    [self.contentView addSubview:self.bgView];
}
- (UIView *)bgView {
    if(!_bgView){
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(10, 0 , SCREEN_W - 20,45)];
        
        _bgView.backgroundColor = COLOR_APP_BACKGROUND;
        [_bgView js_setRoundedCorners:UIRectCornerAllCorners radii:22.5];
        [_bgView addSubview:self.titleLabel];
        [_bgView addSubview:self.stateImg];
    }
    return _bgView;
}
- (UILabel *)titleLabel {
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 11, self.bgView.width, 22)];
        _titleLabel.text = @"2213";
        _titleLabel.textColor = COLOR_BLACK;
        _titleLabel.font = PingFangMediumFont(16);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
- (UIImageView *)stateImg {
    if(!_stateImg){
        _stateImg = [[UIImageView alloc]initWithFrame:CGRectMake(self.bgView.width - 18 -30, 13, 18,18)];
        _stateImg.contentMode = UIViewContentModeScaleAspectFill;
        _stateImg.hidden = YES;//wrongIcon  rightIcon
    }
    return _stateImg;
}

@end

@interface DaTiListView ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>
/*  */
@property (nonatomic, strong) UITableView    * mTableView;
@property (nonatomic, strong) UICollectionView    * myCollectionView;

@property (nonatomic, strong) NSMutableArray    * dataArray;

@end


@implementation DaTiListView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)createUI {
    [self addSubview:self.mTableView];
    [self addSubview:self.myCollectionView];
}
- (void)setIsList:(BOOL)isList {
    _isList = isList;
    self.mTableView.hidden = !isList;
    self.myCollectionView.hidden = isList;
}
- (UITableView *)mTableView {
    if(!_mTableView){
        _mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, self.height) style:UITableViewStylePlain];
        _mTableView.backgroundColor = UIColor.whiteColor;
        _mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mTableView.delegate = self;
        _mTableView.dataSource = self;
        [_mTableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"QLBillCell"];
    }
    return _mTableView;
}

#pragma mark ==================== tabledelegate                ======================
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QLBillCell"];
//    [cell updateItemCellModel:self.dataArray[indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.cellClicked) {
        self.cellClicked();
    }
}


- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        UICollectionViewFlowLayout *myCollectionLayout = [[UICollectionViewFlowLayout alloc]init];
        myCollectionLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        myCollectionLayout.minimumInteritemSpacing = 5;
        myCollectionLayout.minimumLineSpacing = 5;
        myCollectionLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
        myCollectionLayout.itemSize = CGSizeMake((SCREEN_W-25)/2.0f,(SCREEN_W-25)/2.0f*223/175);
        
        _myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, self.height) collectionViewLayout:myCollectionLayout];
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        
        _myCollectionView.backgroundColor = UIColor.whiteColor;
        [_myCollectionView registerClass:UICollectionViewCell.class forCellWithReuseIdentifier:@"UICollectionViewCell"];
        
    }
    return _myCollectionView;
}


#pragma mark collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
//    [cell updateItemCellModel:self.dataArray[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //   ADRechargeCell * cell = (ADRechargeCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //    cell.isChoose = NO;
}


- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        //       ADRechargeCell * displayCell = (ADRechargeCell *)cell;
        //      displayCell.isChoose = YES;
    }
}
//NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//[self.mCollectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:(UICollectionViewScrollPositionNone)];


@end
